<template>
  <el-dialog
    :title="!dataForm.dictCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="280px">
    <el-form-item label="父级编号" prop="parentCode">
      <el-input v-model="dataForm.parentCode" placeholder="父级编号"></el-input>
    </el-form-item>
    <el-form-item label="所有父级编号" prop="parentCodes">
      <el-input v-model="dataForm.parentCodes" placeholder="所有父级编号"></el-input>
    </el-form-item>
    <el-form-item label="本级排序号（升序）" prop="treeSort">
      <el-input v-model="dataForm.treeSort" placeholder="本级排序号（升序）"></el-input>
    </el-form-item>
    <el-form-item label="所有级别排序号" prop="treeSorts">
      <el-input v-model="dataForm.treeSorts" placeholder="所有级别排序号"></el-input>
    </el-form-item>
    <el-form-item label="是否最末级" prop="treeLeaf">
      <el-input v-model="dataForm.treeLeaf" placeholder="是否最末级"></el-input>
    </el-form-item>
    <el-form-item label="层次级别" prop="treeLevel">
      <el-input v-model="dataForm.treeLevel" placeholder="层次级别"></el-input>
    </el-form-item>
    <el-form-item label="全节点名" prop="treeNames">
      <el-input v-model="dataForm.treeNames" placeholder="全节点名"></el-input>
    </el-form-item>
    <el-form-item label="字典标签" prop="dictLabel">
      <el-input v-model="dataForm.dictLabel" placeholder="字典标签"></el-input>
    </el-form-item>
    <el-form-item label="字典键值" prop="dictValue">
      <el-input v-model="dataForm.dictValue" placeholder="字典键值"></el-input>
    </el-form-item>
    <el-form-item label="字典类型" prop="dictType">
      <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
    </el-form-item>
    <el-form-item label="系统内置（1是 0否）" prop="isSys">
      <el-input v-model="dataForm.isSys" placeholder="系统内置（1是 0否）"></el-input>
    </el-form-item>
    <el-form-item label="字典描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="字典描述"></el-input>
    </el-form-item>
    <el-form-item label="css样式（如：color:red)" prop="cssStyle">
      <el-input v-model="dataForm.cssStyle" placeholder="css样式（如：color:red)"></el-input>
    </el-form-item>
    <el-form-item label="css类名（如：red）" prop="cssClass">
      <el-input v-model="dataForm.cssClass" placeholder="css类名（如：red）"></el-input>
    </el-form-item>
    <el-form-item label="状态（0正常 1删除 2停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（0正常 1删除 2停用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          dictCode: 0,
          parentCode: '',
          parentCodes: '',
          treeSort: '',
          treeSorts: '',
          treeLeaf: '',
          treeLevel: '',
          treeNames: '',
          dictLabel: '',
          dictValue: '',
          dictType: '',
          isSys: '',
          description: '',
          cssStyle: '',
          cssClass: '',
          status: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remarks: '',
          corpCode: '',
          corpName: ''
        },
        dataRule: {
          parentCode: [
            { required: true, message: '父级编号不能为空', trigger: 'blur' }
          ],
          parentCodes: [
            { required: true, message: '所有父级编号不能为空', trigger: 'blur' }
          ],
          treeSort: [
            { required: true, message: '本级排序号（升序）不能为空', trigger: 'blur' }
          ],
          treeSorts: [
            { required: true, message: '所有级别排序号不能为空', trigger: 'blur' }
          ],
          treeLeaf: [
            { required: true, message: '是否最末级不能为空', trigger: 'blur' }
          ],
          treeLevel: [
            { required: true, message: '层次级别不能为空', trigger: 'blur' }
          ],
          treeNames: [
            { required: true, message: '全节点名不能为空', trigger: 'blur' }
          ],
          dictLabel: [
            { required: true, message: '字典标签不能为空', trigger: 'blur' }
          ],
          dictValue: [
            { required: true, message: '字典键值不能为空', trigger: 'blur' }
          ],
          dictType: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
          isSys: [
            { required: true, message: '系统内置（1是 0否）不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '字典描述不能为空', trigger: 'blur' }
          ],
          cssStyle: [
            { required: true, message: 'css样式（如：color:red)不能为空', trigger: 'blur' }
          ],
          cssClass: [
            { required: true, message: 'css类名（如：red）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（0正常 1删除 2停用）不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注信息不能为空', trigger: 'blur' }
          ],
          corpCode: [
            { required: true, message: '租户代码不能为空', trigger: 'blur' }
          ],
          corpName: [
            { required: true, message: '租户名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.dictCode = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dictCode) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dictdata/info/${this.dataForm.dictCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentCode = data.dictData.parentCode
                this.dataForm.parentCodes = data.dictData.parentCodes
                this.dataForm.treeSort = data.dictData.treeSort
                this.dataForm.treeSorts = data.dictData.treeSorts
                this.dataForm.treeLeaf = data.dictData.treeLeaf
                this.dataForm.treeLevel = data.dictData.treeLevel
                this.dataForm.treeNames = data.dictData.treeNames
                this.dataForm.dictLabel = data.dictData.dictLabel
                this.dataForm.dictValue = data.dictData.dictValue
                this.dataForm.dictType = data.dictData.dictType
                this.dataForm.isSys = data.dictData.isSys
                this.dataForm.description = data.dictData.description
                this.dataForm.cssStyle = data.dictData.cssStyle
                this.dataForm.cssClass = data.dictData.cssClass
                this.dataForm.status = data.dictData.status
                this.dataForm.createBy = data.dictData.createBy
                this.dataForm.createDate = data.dictData.createDate
                this.dataForm.updateBy = data.dictData.updateBy
                this.dataForm.updateDate = data.dictData.updateDate
                this.dataForm.remarks = data.dictData.remarks
                this.dataForm.corpCode = data.dictData.corpCode
                this.dataForm.corpName = data.dictData.corpName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dictdata/${!this.dataForm.dictCode ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'dictCode': this.dataForm.dictCode || undefined,
                'parentCode': this.dataForm.parentCode,
                'parentCodes': this.dataForm.parentCodes,
                'treeSort': this.dataForm.treeSort,
                'treeSorts': this.dataForm.treeSorts,
                'treeLeaf': this.dataForm.treeLeaf,
                'treeLevel': this.dataForm.treeLevel,
                'treeNames': this.dataForm.treeNames,
                'dictLabel': this.dataForm.dictLabel,
                'dictValue': this.dataForm.dictValue,
                'dictType': this.dataForm.dictType,
                'isSys': this.dataForm.isSys,
                'description': this.dataForm.description,
                'cssStyle': this.dataForm.cssStyle,
                'cssClass': this.dataForm.cssClass,
                'status': this.dataForm.status,
                'createBy': this.dataForm.createBy,
                'createDate': this.dataForm.createDate,
                'updateBy': this.dataForm.updateBy,
                'updateDate': this.dataForm.updateDate,
                'remarks': this.dataForm.remarks,
                'corpCode': this.dataForm.corpCode,
                'corpName': this.dataForm.corpName,
                'extendS1': this.dataForm.extendS1,
                'extendS2': this.dataForm.extendS2,
                'extendS3': this.dataForm.extendS3,
                'extendS4': this.dataForm.extendS4,
                'extendS5': this.dataForm.extendS5,
                'extendS6': this.dataForm.extendS6,
                'extendS7': this.dataForm.extendS7,
                'extendS8': this.dataForm.extendS8,
                'extendI1': this.dataForm.extendI1,
                'extendI2': this.dataForm.extendI2,
                'extendI3': this.dataForm.extendI3,
                'extendI4': this.dataForm.extendI4,
                'extendF1': this.dataForm.extendF1,
                'extendF2': this.dataForm.extendF2,
                'extendF3': this.dataForm.extendF3,
                'extendF4': this.dataForm.extendF4,
                'extendD1': this.dataForm.extendD1,
                'extendD2': this.dataForm.extendD2,
                'extendD3': this.dataForm.extendD3,
                'extendD4': this.dataForm.extendD4
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
